#!/bin/sh
#
# Start uMTPrd....
#

start() {
    printf "Starting ADBD: "
    mount none /sys/kernel/config -t configfs
    mkdir /sys/kernel/config/usb_gadget/g1
    cd /sys/kernel/config/usb_gadget/g1
    mkdir configs/c.1
    mkdir functions/ffs.adb
    mkdir strings/0x409
    mkdir configs/c.1/strings/0x409

    echo 0x1d06 > idProduct
    echo 0x1802 > idVendor
    echo 0x0100 >  bcdDevice

    echo "akubox" > strings/0x409/serialnumber
    echo "AKU" > strings/0x409/manufacturer
    echo "aku box" > strings/0x409/product

    echo "adb+xxxx" > configs/c.1/strings/0x409/configuration
    echo 500 > configs/c.1/MaxPower
    echo 0xc0 > configs/c.1/bmAttributes

    killall adbd
    ln -s functions/ffs.adb configs/c.1
    mkdir -p /dev/usb-ffs/adb
    mount -t functionfs adb /dev/usb-ffs/adb
    start-stop-daemon -S -q -m -b -p /var/run/adb.pid -x /usr/bin/adbd
    sleep 1
    cd /root/
    ls /sys/class/udc/ > /sys/kernel/config/usb_gadget/g1/UDC
    [ $? = 0 ] && echo "OK" || echo "FAIL"
}

stop() {
    printf "Stopping ADBD: "
    echo > /sys/kernel/config/usb_gadget/g1/UDC
    start-stop-daemon -K -q -p /var/run/adb.pid -x /usr/bin/adbd
    [ $? = 0 ] && echo "OK" || echo "FAIL"
}

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    restart|reload)
    stop
    sleep 1
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit $?
